﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Tools.Maptools.Editor;

[CustomEditor(typeof(TrafficPoint))]
public class TrafficPointEditor : Editor
{
    private TrafficPoint _trafficPoint;
    private TrafficPoint _tagertPoint;

    private GameObject _tagertObj;
    private Transform _curLineRoot;
    private int roadNum = 1;
    public override void OnInspectorGUI()
    {
        _trafficPoint = (TrafficPoint)target;
        if (_trafficPoint.isRoadPoint) return;
        base.OnInspectorGUI();
        GUILayout.BeginHorizontal();
        GUILayout.Label("中间点数量：", GUILayout.Width(80));
        roadNum = EditorGUILayout.IntField(roadNum, GUILayout.Width(50));
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.Label("目标点：", GUILayout.Width(80));
        _tagertObj = EditorGUILayout.ObjectField( _tagertObj, typeof(GameObject), true, GUILayout.Width(120)) as GameObject;
        GUILayout.EndHorizontal();
        if (_tagertObj)
        {
            _tagertPoint = _tagertObj.GetComponent<TrafficPoint>();
        }

        if (GUILayout.Button("生成路线节点(直线不用生成)") && _tagertPoint)
        {
            Transform root = _trafficPoint.transform.Find(_tagertPoint.pointId.ToString());
            if (root == null)
            {
                GameObject newRoot = new GameObject();
                newRoot.name = _tagertPoint.pointId.ToString();
                newRoot.transform.parent = _trafficPoint.transform;
                root = newRoot.transform;
            }
            _curLineRoot = root;
            Vector3 disVec3 = (_tagertObj.transform.position - _trafficPoint.transform.position) / (roadNum + 1);
            for (int i = 0;i < roadNum;i++)
            {
                GameObject obj = new GameObject();
                obj.name = "roadPoint_" + i;
                TrafficPoint point = obj.AddComponent<TrafficPoint>();
                point.pointId = 1;
                point.isRoadPoint = true;
                obj.transform.parent = _curLineRoot;
                // 设置位置
                obj.transform.position = _tagertObj.transform.position + disVec3 * (i + 1);
            }
        }
    }
}

